package org.tote.command;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;

import org.hibernate.validator.constraints.Email;
import org.tote.model.User;
import org.tote.model.UserLight;
import org.tote.util.Constant;

/**
 * Representation of User general info for the view layer
 * 
 */

public class UserCommand implements Serializable {

	private static final long serialVersionUID = 445204163686713524L;

	private Long id;
	private String username;
	//@NotBlank
	private String password;
	private String newPassword;
	private String confirmedPassword;

	private Integer status;
	private String fullnameRu;
	private String fullnameEn;
	private Date dateRegistered;
	private String token;
	private String locale;
	private String location;
	@Email
	private String email;
	private String phone;
	private byte[] avatar;
	private String avatarType;
	private Date updTime;
	private User updUser;
	
	////////////
	private Boolean selected = false;
	private Long fromUserId; // for duplicate user

	public UserCommand() {
	}

	public UserCommand(User user) {
		if (user == null) {
			throw new IllegalArgumentException(
					"User cannot be null when constructing new User command");
		}
		this.id = user.getId();
		this.username = user.getUsername();
		this.password = user.getPassword();
		this.status = user.getStatus();
		this.avatar = user.getAvatar();
		this.avatarType = user.getAvatarType();
		this.dateRegistered = user.getDateRegistered();
		this.token = user.getToken();
		this.fullnameEn = user.getFullnameEn();
		this.fullnameRu = user.getFullnameRu();
		this.locale = user.getLocale();
		this.location = user.getLocation();
		this.email = user.getEmail();
		this.phone = user.getPhone();
		this.updTime = user.getUpdTime();
		this.updUser = user.getUpdUser();
		//this.childUsers = user.getChildUsers();
		//this.roles = user.getRoles();
	}

	public UserCommand(UserLight user) {
		if (user == null) {
			throw new IllegalArgumentException(
					"User cannot be null when constructing new User command");
		}
		this.id = user.getId();
		setName(user.getName());
	}

	public UserLight getAsUserLight() {
		UserLight user = new UserLight();
		user.setId(id);
		user.setName(getName());
		return user;
	}

	public User getAsUser() {
		User user = new User();
		user.setId(id);
		user.setUsername(username);
		user.setPassword(password);
		user.setStatus(status);
		user.setAvatar(avatar);
		user.setAvatarType(avatarType);
		user.setDateRegistered(dateRegistered);
		user.setToken(token);
		user.setFullnameEn(fullnameEn);
		user.setFullnameRu(fullnameRu);
		user.setLocale(locale);
		user.setLocation(location);
		user.setEmail(email);
		user.setPhone(phone);
		user.setUpdTime(updTime);
		user.setUpdUser(updUser);
		return user;
	}

	public String getName() {
		return Constant.getMessage(locale, fullnameRu, fullnameEn);
	}

	public void setName(String name) {
		fullnameEn = name;
		fullnameRu = name;
	}

	public Boolean getEnabled() {
		return status != null && status == Constant.STATUS_READY ? true : false;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmedPassword() {
		return confirmedPassword;
	}

	public void setConfirmedPassword(String confirmedPassword) {
		this.confirmedPassword = confirmedPassword;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getFullnameRu() {
		return fullnameRu;
	}

	public void setFullnameRu(String fullnameRu) {
		this.fullnameRu = fullnameRu;
	}

	public String getFullnameEn() {
		return fullnameEn;
	}

	public void setFullnameEn(String fullnameEn) {
		this.fullnameEn = fullnameEn;
	}

	public Date getDateRegistered() {
		return dateRegistered;
	}

	public void setDateRegistered(Date dateRegistered) {
		this.dateRegistered = dateRegistered;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getLocale() {
		return locale;
	}

	public void setLocale(String locale) {
		this.locale = locale;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public byte[] getAvatar() {
		return avatar;
	}

	public void setAvatar(byte[] avatar) {
		this.avatar = avatar;
	}

	public String getAvatarType() {
		return avatarType;
	}

	public void setAvatarType(String avatarType) {
		this.avatarType = avatarType;
	}

	public Date getUpdTime() {
		return updTime;
	}

	public void setUpdTime(Date updTime) {
		this.updTime = updTime;
	}

	public User getUpdUser() {
		return updUser;
	}

	public void setUpdUser(User updUser) {
		this.updUser = updUser;
	}

	public Long getFromUserId() {
		return fromUserId;
	}

	public void setFromUserId(Long fromUserId) {
		this.fromUserId = fromUserId;
	}

	public Boolean getSelected() {
		return selected;
	}

	public void setSelected(Boolean selected) {
		this.selected = selected;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(avatar);
		result = prime * result + ((avatarType == null) ? 0 : avatarType.hashCode());
		result = prime * result + ((dateRegistered == null) ? 0 : dateRegistered.hashCode());
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result + ((fullnameEn == null) ? 0 : fullnameEn.hashCode());
		result = prime * result + ((fullnameRu == null) ? 0 : fullnameRu.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((locale == null) ? 0 : locale.hashCode());
		result = prime * result + ((location == null) ? 0 : location.hashCode());
		result = prime * result + ((newPassword == null) ? 0 : newPassword.hashCode());
		result = prime * result + ((confirmedPassword == null) ? 0 : confirmedPassword.hashCode());
		result = prime * result + ((password == null) ? 0 : password.hashCode());
		result = prime * result + ((phone == null) ? 0 : phone.hashCode());
		result = prime * result + ((status == null) ? 0 : status.hashCode());
		result = prime * result + ((token == null) ? 0 : token.hashCode());
		result = prime * result + ((username == null) ? 0 : username.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (obj == null) return false;
		if (getClass() != obj.getClass()) return false;
		UserCommand other = (UserCommand) obj;
		if (!Arrays.equals(avatar, other.avatar)) return false;
		if (avatarType == null) {
			if (other.avatarType != null) return false;
		} else if (!avatarType.equals(other.avatarType)) return false;
		if (dateRegistered == null) {
			if (other.dateRegistered != null) return false;
		} else if (!dateRegistered.equals(other.dateRegistered)) return false;
		if (email == null) {
			if (other.email != null) return false;
		} else if (!email.equals(other.email)) return false;
		if (fullnameEn == null) {
			if (other.fullnameEn != null) return false;
		} else if (!fullnameEn.equals(other.fullnameEn)) return false;
		if (fullnameRu == null) {
			if (other.fullnameRu != null) return false;
		} else if (!fullnameRu.equals(other.fullnameRu)) return false;
		if (id == null) {
			if (other.id != null) return false;
		} else if (!id.equals(other.id)) return false;
		if (locale == null) {
			if (other.locale != null) return false;
		} else if (!locale.equals(other.locale)) return false;
		if (location == null) {
			if (other.location != null) return false;
		} else if (!location.equals(other.location)) return false;
		if (newPassword == null) {
			if (other.newPassword != null) return false;
		} else if (!newPassword.equals(other.newPassword)) return false;
		if (confirmedPassword == null) {
			if (other.confirmedPassword != null) return false;
		} else if (!confirmedPassword.equals(other.confirmedPassword)) return false;
		if (password == null) {
			if (other.password != null) return false;
		} else if (!password.equals(other.password)) return false;
		if (phone == null) {
			if (other.phone != null) return false;
		} else if (!phone.equals(other.phone)) return false;
		if (status == null) {
			if (other.status != null) return false;
		} else if (!status.equals(other.status)) return false;
		if (token == null) {
			if (other.token != null) return false;
		} else if (!token.equals(other.token)) return false;
		if (username == null) {
			if (other.username != null) return false;
		} else if (!username.equals(other.username)) return false;
		return true;
	}

	@Override
	public String toString() {
		return "UserCommand [id=" + id + ", username=" + username + ", status=" + status + "]";
	}

}
